package edu.hawaii.jmotif.sequitur.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Observable;

import javax.swing.JFileChooser;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;

import edu.hawaii.jmotif.sequitur.model.SequiturMessage;
import edu.hawaii.jmotif.sequitur.model.SequiturModel;

public class SequiturController extends Observable implements ActionListener {

  private SequiturModel model;

  public SequiturController(SequiturModel model) {
    super();
    this.model = model;
  }

  public ActionListener getSelectFileListener() {

    ActionListener selectDataActionListener = new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Select Data File");

        String filename = model.getCurrentDataFile();
        if (!((null == filename) || filename.isEmpty())) {
          fileChooser.setSelectedFile(new File(filename));
        }

        if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
          File file = fileChooser.getSelectedFile();
          model.setCurrentDataFile(file.getAbsolutePath());
        }
      }

    };
    return selectDataActionListener;
  }
  
  public DocumentListener getFilenameListener() {
	    DocumentListener filenamedocumentListener = new DocumentListener() {
			public void changedUpdate(DocumentEvent e) {
				handleDocumentEvent(e);
			}
			
			public void removeUpdate(DocumentEvent e) {
				handleDocumentEvent(e);
			}
			public void insertUpdate(DocumentEvent e) {
				handleDocumentEvent(e);
			}
			private void handleDocumentEvent(DocumentEvent e) {
				String filename = null;
				 try {
					 filename = e.getDocument().getText(0, e.getDocument().getLength());
				 } catch( BadLocationException ble){
					 
				 }
				 model.setDataFileName(filename);
			}
	    };
	    return filenamedocumentListener;
	  }

  public ActionListener getLoadFileListener() {
    ActionListener loadDataActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        model.loadData(e.getActionCommand());
      }
    };
    return loadDataActionListener;
  }

  public ActionListener getProcessDataListener() {
    ActionListener loadDataActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        String[] split = e.getActionCommand().split(",");
        int windowSize = Integer.parseInt(split[0]);
        int paaSize = Integer.parseInt(split[1]);
        int alphabetSize = Integer.parseInt(split[2]);

        model.processData(windowSize, paaSize, alphabetSize);
      }
    };
    return loadDataActionListener;
  }

  public ActionListener getDisplayCommandListener() {
    ActionListener displayStuffActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        model.displayStuff();
      }
    };
    return displayStuffActionListener;
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    this.setChanged();
    notifyObservers(new SequiturMessage(SequiturMessage.STATUS_MESSAGE,
        "controller: Unknown action performed " + e.getActionCommand()));
  }
}
